var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
import { ref, defineComponent, onMounted, onActivated, resolveComponent, createElementBlock, openBlock, createVNode, withCtx, createElementVNode, unref, createTextVNode, createBlock, createCommentVNode } from "vue";
import { defineStore } from "pinia";
import { ElMessageBox, ElMessage } from "element-plus";
import { Plus } from "@element-plus/icons-vue";
const SEND_TASK_CMD_KEY = "send-task-cmd";
const GET_TASK_DATA_KEY = "get-task-data";
const useTaskStore = defineStore("taskInjection", () => {
  const sendTaskCmd = ref(null);
  const setInjections = (injections) => {
    if (injections.sendTaskCmd) {
      sendTaskCmd.value = injections.sendTaskCmd;
    }
  };
  return { sendTaskCmd, setInjections };
});
function createCommand(moduleId, fixedParams) {
  return async function(data, params, config2 = {}) {
    const calibStore = useTaskStore();
    const sendTaskCmd = calibStore.sendTaskCmd;
    if (!sendTaskCmd) {
      throw new Error("sendTaskCmd 未在 Pinia 中初始化，请检查插件根组件是否正常挂载");
    }
    const commandPayload = {
      moduleId,
      fixedParams,
      data,
      params: { ...fixedParams, ...params },
      config: config2
    };
    const result = await sendTaskCmd("execute", commandPayload);
    return result;
  };
}
const moudleType = "TaskForm";
const getTaskListByCondition = createCommand(moudleType, { commandName: "GetTaskListByCondition" });
const routerLinkTestView = createCommand(moudleType, { commandName: "RouterLink" });
const deleteTask = createCommand(moudleType, { commandName: "DeleteTask" });
const getTaskSolutions = createCommand(moudleType, { commandName: "GetTaskSolutions" });
const getAllManufacturers = createCommand(moudleType, { commandName: "GetAllManufacturers" });
const _hoisted_1 = { class: "list-view" };
const _hoisted_2 = { class: "filter-view" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  ...{
    name: "TaskView",
    components: {}
  },
  __name: "index",
  setup(__props, { expose: __expose }) {
    const platformNodeTree = ref(null);
    ref(null);
    onMounted(async () => {
      try {
        const hostModule = window.platformComponents || {};
        const NodeTree = hostModule.NodeTree || hostModule.default;
        if (NodeTree && NodeTree.NodeTree) {
          platformNodeTree.value = NodeTree.NodeTree;
        } else {
          console.warn("平台组件未找到，使用降级方案");
        }
      } catch (error) {
        console.error("加载平台模块失败:", error);
      }
    });
    const assd = ref(null);
    const dataEvent = (data) => {
      console.log("子组件收到数据了", data);
      assd.value = data;
    };
    __expose({
      dataEvent
    });
    const taskStore = useTaskStore();
    console.log("存储后 Pinia 中的值：", taskStore.sendTaskCmd);
    const queryFormRef = ref();
    const query = ref({ pageIndex: 1, pageSize: 30, manufactureName: "", solutionName: "", taskName: "", testTime: "" });
    const state = ref("");
    const total = ref(0);
    const tableConfig = [
      {
        label: "任务名称",
        prop: "taskName",
        type: "link",
        onClick: async (row) => {
          console.log("行数据：准备跳转", row);
          await routerLinkTestView("/testView?taskId=" + row.taskId);
        }
      },
      { label: "设备型号", prop: "dutMode" },
      { label: "统计信息", prop: "testProcess", type: "slot", slot: "progress" },
      { label: "解决方案", prop: "solutionName" },
      { label: "设备厂家", prop: "manufacturer" },
      { label: "检测日期", prop: "taskTestTime" }
    ];
    const tableData = ref([]);
    const queryFilterConfig = ref([
      { placeholder: "请输入任务名称", prop: "taskName" },
      { placeholder: "请选择解决方案", prop: "solutionName", type: "select", options: [] },
      { placeholder: "请选择设备厂家", prop: "manufactureName", type: "select", options: [] },
      { placeholder: "请选择检测日期", prop: "testTime", type: "date", format: "YYYY/MM/DD", valueFormat: "YYYY/MM/DD" }
    ]);
    onActivated(() => {
      console.log("准备渲染插件页面");
      getList();
      getSolutions();
      getFacturers();
    });
    const getList = async () => {
      state.value = "loading";
      const {
        data: { list, total_Count }
      } = await getTaskListByCondition(query.value);
      console.log("获取的数据", list);
      state.value = "";
      tableData.value = (list || []).map((it) => {
        it.testProcess = isNaN(Number(it.testProcess)) ? "0" : it.testProcess;
        return it;
      });
      total.value = total_Count || 0;
    };
    const getSolutions = async () => {
      const { data } = await getTaskSolutions({});
      const row = queryFilterConfig.value.find((it) => it.prop === "solutionName");
      if (row) {
        row.options = (data || []).map((it) => {
          return { label: it, value: it };
        });
      }
    };
    const getFacturers = async () => {
      const { data } = await getAllManufacturers({});
      const row = queryFilterConfig.value.find((it) => it.prop === "manufactureName");
      if (row) {
        row.options = (data || []).map((it) => {
          return { label: it, value: it };
        });
      }
    };
    const onDelete = async (row) => {
      await ElMessageBox({
        title: "提示",
        message: `是否确定删除任务“${row.taskName}”?`,
        type: "warning",
        showCancelButton: true,
        beforeClose: async (action, instance, done) => {
          if (action === "confirm") {
            try {
              instance.confirmButtonLoading = true;
              instance.confirmButtonText = `执行中...`;
              await deleteTask({ taskId: row.taskId });
              done();
            } catch {
              instance.confirmButtonText = `确定`;
            }
            instance.confirmButtonLoading = false;
          } else {
            done();
          }
        }
      });
      ElMessage.closeAll();
      ElMessage.success(`删除成功`);
      getList();
    };
    const progressColor = (val) => {
      return val < 80 ? val < 50 ? "#f56c6c" : "#e6a23c" : "";
    };
    const toAdd = async () => {
      console.log("跳转新建任务");
      await routerLinkTestView("/addTask");
    };
    return (_ctx, _cache) => {
      const _component_el_link = resolveComponent("el-link");
      const _component_el_button = resolveComponent("el-button");
      const _component_el_progress = resolveComponent("el-progress");
      const _component_XcTable = resolveComponent("XcTable");
      return openBlock(), createElementBlock("div", _hoisted_1, [
        createVNode(_component_XcTable, {
          class: "is-list",
          ref: "tableView",
          tableKey: _ctx.$route.path,
          page: query.value.pageIndex,
          "onUpdate:page": _cache[0] || (_cache[0] = ($event) => query.value.pageIndex = $event),
          limit: query.value.pageSize,
          "onUpdate:limit": _cache[1] || (_cache[1] = ($event) => query.value.pageSize = $event),
          pageTotal: total.value,
          config: tableConfig,
          state: state.value,
          values: tableData.value,
          formRef: queryFormRef.value,
          operationWidth: "100px",
          pagingConfig: { background: true },
          onUpdate: getList
        }, {
          default: withCtx(({ row }) => [
            row.account !== "admin" ? (openBlock(), createBlock(_component_el_link, {
              key: 0,
              size: "small",
              type: "danger",
              underline: false,
              onClick: ($event) => onDelete(row)
            }, {
              default: withCtx(() => [..._cache[2] || (_cache[2] = [
                createTextVNode("删除", -1)
              ])]),
              _: 2
            }, 1032, ["onClick"])) : createCommentVNode("", true)
          ]),
          actions: withCtx(() => [
            createElementVNode("div", _hoisted_2, [
              createVNode(_component_el_button, {
                type: "primary",
                icon: unref(Plus),
                onClick: toAdd
              }, {
                default: withCtx(() => [..._cache[3] || (_cache[3] = [
                  createTextVNode("新建任务重新修改666", -1)
                ])]),
                _: 1
              }, 8, ["icon"])
            ])
          ]),
          progress: withCtx(({ row }) => [
            createVNode(_component_el_progress, {
              percentage: Number(row.testProcess),
              color: progressColor(row.testProcess)
            }, null, 8, ["percentage", "color"])
          ]),
          _: 1
        }, 8, ["tableKey", "page", "limit", "pageTotal", "state", "values", "formRef"])
      ]);
    };
  }
});
const _export_sfc = (sfc, props) => {
  const target = sfc.__vccOpts || sfc;
  for (const [key, val] of props) {
    target[key] = val;
  }
  return target;
};
const TaskForm = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-0867f199"]]);
const config = {
  moudleType: "TaskForm",
  name: "任务模块",
  version: "1.0.0",
  description: "任务列表查询"
};
class TaskModule {
  constructor(sendCmd, initialData = {}) {
    __publicField(this, "config", config);
    __publicField(this, "_eventData");
    __publicField(this, "sendCmd");
    this._eventData = ref(initialData);
    this.sendCmd = sendCmd;
  }
  sendCommand(cmd, cmdParams) {
    console.log("发送指令：", cmd, cmdParams);
    return this.sendCmd(this.config.moudleType, cmd, cmdParams);
  }
  install(app) {
    app.component("TaskForm", TaskForm);
    app.provide(GET_TASK_DATA_KEY, () => this.eventData);
    app.provide(SEND_TASK_CMD_KEY, (cmd, cmdParams) => {
      console.log("插件内部转发指令：", cmd, cmdParams);
      return this.sendCommand(cmd, cmdParams);
    });
  }
  get eventData() {
    return this._eventData;
  }
  dataEvent(data) {
    Object.assign(this._eventData, data);
  }
  async startup() {
    const taskStore = useTaskStore();
    if (!taskStore.sendTaskCmd) {
      console.log("Pinia 中未存储 sendTaskCmd,进行存储");
      taskStore.setInjections({
        sendTaskCmd: (cmd, cmdParams) => this.sendCommand(cmd, cmdParams)
      });
    }
    console.log("插件根组件获取到 sendTaskCmd,初始化成功", taskStore.sendTaskCmd);
    console.log("存储后 Pinia 中的值：", taskStore.sendTaskCmd);
    console.log("任务模块初始化完成");
  }
}
const createTaskModule = (sendCmd, initialData) => {
  return new TaskModule(sendCmd, initialData);
};
export {
  TaskForm,
  TaskModule,
  createTaskModule,
  createTaskModule as default
};
